package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.molichuxing.services.infrastructure.bean.AccidentCaseBean;
import com.molichuxing.services.infrastructure.bean.AccidentImageBean;
import com.molichuxing.services.infrastructure.bean.AccidentImgBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.AccidentCaseMapper;
import com.molichuxing.services.infrastructure.dao.AccidentImageMapper;
import com.molichuxing.services.infrastructure.dao.AccidentImgBeanMapper;
import com.molichuxing.services.infrastructure.dto.request.create.AccidentImageCreateDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentCaseDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentImageDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.InsuranceDto;
import com.molichuxing.services.infrastructure.service.AccidentCaseService;
import com.molichuxing.services.infrastructure.service.AccidentImageService;
import com.molichuxing.services.property.CarAccidentImageEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 事故 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("accidentImageService")
public class AccidentImageServiceImpl implements AccidentImageService {

  private static final Logger log = Logger.getLogger(AccidentImageServiceImpl.class);

  @Resource
  private AccidentImageMapper accidentImageMapper;

  @Resource
  private AccidentImgBeanMapper accidentImgBeanMapper;


    @Override
    public Map<Integer, List<AccidentImageDto>> getCarAccidentImageList(Integer accidentId, List<CarAccidentImageEnum> carAccidentImageEnum) {
        List<Integer> types = null;
        if(null != carAccidentImageEnum&& carAccidentImageEnum.size() > 0 ) {
            types = new ArrayList<>();
            for(CarAccidentImageEnum enums : carAccidentImageEnum) {
                types.add(enums.getValue());
            }
        }
        List<AccidentImageBean> result = accidentImageMapper.getCarAccidentImageList(accidentId, types);
        if((null == result)||(result.size() ==0)) {
            return null;
        }
        return Convert.toAccidentImageDto(result).stream().collect(Collectors.groupingBy(AccidentImageDto::getType));
    }

    @Override
    public Boolean createAccidentImage(AccidentImageCreateDto accidentImageCreateDto) {
        int result = accidentImageMapper.createAccidentImage(Convert.toAccidentImageBeanBean(accidentImageCreateDto));
        if(result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteAccidentImage(Integer accidentId, Integer type) {
        int result = accidentImageMapper.deleteAccidentImage(accidentId,type);
        if(result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void selectOldData() {
        List<AccidentImgBean> oldDataList = accidentImgBeanMapper.selectAll();
        List<String> sqlList = new ArrayList<>();
        for(AccidentImgBean accidentImgBean : oldDataList) {
            if(null != accidentImgBean.getLiabilityLetterType() && 0!=accidentImgBean.getLiabilityLetterType()&&(!accidentImgBean.getLiabilityImg().equals("[]"))) {
                StringBuffer sb = new StringBuffer() ;
                sb.append("update cars_accident set type = '");
                sb.append(accidentImgBean.getLiabilityLetterType()+"',");
                sb.append("path = '");
                sb.append(JSONObject.parseArray(accidentImgBean.getLiabilityImg()).get(0)+"'");
                sb.append(" where id = "+accidentImgBean.getAccidentId());
                sqlList.add(sb.toString());
            }
        }
        List<String> imgSqlList = new ArrayList<>();
        for(AccidentImgBean accidentImgBean : oldDataList) {
            if(StringUtils.isNotEmpty(accidentImgBean.getVehicleImg())) {
                JSONObject vehicleImgJSON = JSONObject.parseObject(accidentImgBean.getVehicleImg());
                Object vehicle_45_degree_angleObject = vehicleImgJSON.get("vehicle_45_degree_angle");
               if(null != vehicle_45_degree_angleObject) {
                   JSONArray vehicle_45_degree_angleObjectArray = JSONObject.parseArray(JSON.toJSONString(vehicle_45_degree_angleObject));
                   if(null != vehicle_45_degree_angleObjectArray && vehicle_45_degree_angleObjectArray.size() > 0) {
                       for(Object str : vehicle_45_degree_angleObjectArray) {
                           String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.VEHICLE_DEGREE_ANGLE.getValue()+"','"+str+"')";
                           append(sql);
                       }
                   }
               }



                Object vehicle_damageObject = vehicleImgJSON.get("vehicle_damage");
                if(null != vehicle_damageObject) {
                    JSONArray vehicle_damageArray = JSONObject.parseArray(JSON.toJSONString(vehicle_damageObject));
                    if(null != vehicle_damageArray && vehicle_damageArray.size() > 0) {
                        for(Object str : vehicle_damageArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.CAR_DAMAGE_PHOTOS.getValue()+"','"+str+"')";
                           append(sql);
                        }
                    }
                }


                Object accident_panoramaObject = vehicleImgJSON.get("accident_panorama");
                if(null != accident_panoramaObject) {
                    JSONArray accident_panoramaArray = JSONObject.parseArray(JSON.toJSONString(accident_panoramaObject));
                    if(null != accident_panoramaArray && accident_panoramaArray.size() > 0) {
                        for(Object str : accident_panoramaArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.ACCIDENT_PANORAMA  .getValue()+"','"+str+"')";
                           append(sql);
                        }
                    }
                }


                Object collision_detailsObject = vehicleImgJSON.get("collision_details");
                if(null != collision_detailsObject) {
                    JSONArray collision_detailsArray = JSONObject.parseArray(JSON.toJSONString(collision_detailsObject));
                    if(null != collision_detailsArray && collision_detailsArray.size() > 0) {
                        for(Object str : collision_detailsArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.COLLISION_DETAILS  .getValue()+"','"+str+"')";
                           append(sql);
                        }
                    }
                }

                Object scattered_partsObject = vehicleImgJSON.get("scattered_parts");
                if(null != scattered_partsObject) {
                    JSONArray scattered_partsArray = JSONObject.parseArray(JSON.toJSONString(scattered_partsObject));
                    if(null != scattered_partsArray && scattered_partsArray.size() > 0) {
                        for(Object str : scattered_partsArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.SCATTERED_PARTS  .getValue()+"','"+str+"')";
                           append(sql);
                        }
                    }
                }


                Object vinObject = vehicleImgJSON.get("vin");
                if(null != vinObject) {
                    JSONArray vinArray = JSONObject.parseArray(JSON.toJSONString(vinObject));
                    if(null != vinArray && vinArray.size() > 0) {
                        for(Object str : vinArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.FRAME_NUMBER_PHOTO  .getValue()+"','"+str+"')";
                           append(sql);
                        }
                    }
                }

                Object interiorObject = vehicleImgJSON.get("interior");
                if(null != interiorObject) {
                    JSONArray interiorArray = JSONObject.parseArray(JSON.toJSONString(interiorObject));
                    if(null != interiorArray && interiorArray.size() > 0) {
                        for(Object str : interiorArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.INTERIOR_PHOTO.getValue()+"','"+str+"')";
                            append(sql);
                        }
                    }
                }
                //++++++++++++++++++++++++++++++++++++++++++++++++

                JSONObject reportCasualtiesImgJSON = JSONObject.parseObject(accidentImgBean.getReportCasualtiesImg());
                Object injured_panoramaObject = reportCasualtiesImgJSON.get("injured_panorama");
                if(null != injured_panoramaObject) {
                    JSONArray injured_panoramaArray = JSONObject.parseArray(JSON.toJSONString(injured_panoramaObject));
                    if(null != injured_panoramaArray && injured_panoramaArray.size() > 0) {
                        for(Object str : injured_panoramaArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.PANORAMA_WOUNDED.getValue()+"','"+str+"')";
                            append(sql);
                        }
                    }
                }

                Object injured_site_detailsObject = reportCasualtiesImgJSON.get("injured_site_details");
                if(null != injured_site_detailsObject) {
                    JSONArray injured_site_detailsArray = JSONObject.parseArray(JSON.toJSONString(injured_site_detailsObject));
                    if(null != injured_site_detailsArray && injured_site_detailsArray.size() > 0) {
                        for(Object str : injured_site_detailsArray) {
                            String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.DETAILS_INJURED_AREA.getValue()+"','"+str+"')";
                            append(sql);
                        }
                    }
                }
                //++++++++++++++++++++++++++++++++++++++++++++++++
                JSONObject settleDataImgJSON = JSONObject.parseObject(accidentImgBean.getSettleDataImg());
                if(null != settleDataImgJSON) {
                    Object accident_repair_settlementObject = settleDataImgJSON.get("accident_repair_settlement");
                    if(null != accident_repair_settlementObject) {
                        JSONArray accident_repair_settlementArray = JSONObject.parseArray(JSON.toJSONString(accident_repair_settlementObject));
                        if(null != accident_repair_settlementArray && accident_repair_settlementArray.size() > 0) {
                            for(Object str : accident_repair_settlementArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.ACCIDENT_REPAIR_STATEMENT.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }


                    Object maintenance_invoiceObject = settleDataImgJSON.get("maintenance_invoice");
                    if(null != maintenance_invoiceObject) {
                        JSONArray maintenance_invoiceArray = JSONObject.parseArray(JSON.toJSONString(maintenance_invoiceObject));
                        if(null != maintenance_invoiceArray && maintenance_invoiceArray.size() > 0) {
                            for(Object str : maintenance_invoiceArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.MAINTENANCE_INVOICE.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }


                    Object vehicle_repairedObject = settleDataImgJSON.get("vehicle_repaired");
                    if(null != vehicle_repairedObject) {
                        JSONArray vehicle_repairedArray = JSONObject.parseArray(JSON.toJSONString(vehicle_repairedObject));
                        if(null != vehicle_repairedArray && vehicle_repairedArray.size() > 0) {
                            for(Object str : vehicle_repairedArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.PHOTOGRAPHS_AFTER_VEHICLE_REPAIR.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }

                    Object signatory_bank_card_frontObject = settleDataImgJSON.get("signatory_bank_card_front");
                    if(null != signatory_bank_card_frontObject) {
                        JSONArray signatory_bank_card_frontArray = JSONObject.parseArray(JSON.toJSONString(signatory_bank_card_frontObject));
                        if(null != signatory_bank_card_frontArray && signatory_bank_card_frontArray.size() > 0) {
                            for(Object str : signatory_bank_card_frontArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.POSITIVE_PHOTO_BANK_CARD.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }
                }

                //++++++++++++++++++++++++++++++++++++++++++++++++
                JSONObject settleCasualtiesImgJSON = JSONObject.parseObject(accidentImgBean.getSettleCasualtiesImg());
                if(null != settleCasualtiesImgJSON) {
                    Object medical_fee_billsObject = settleCasualtiesImgJSON.get("medical_fee_bills");
                    if(null != medical_fee_billsObject) {
                        JSONArray medical_fee_billsArray = JSONObject.parseArray(JSON.toJSONString(medical_fee_billsObject));
                        if(null != medical_fee_billsArray && medical_fee_billsArray.size() > 0) {
                            for(Object str : medical_fee_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.CERTIFICATE_MEDICAL_EXPENSES.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }




                    Object human_injury_materialObject = settleCasualtiesImgJSON.get("human_injury_material");
                    if(null != human_injury_materialObject) {
                        JSONArray human_injury_materialArray = JSONObject.parseArray(JSON.toJSONString(human_injury_materialObject));
                        if(null != human_injury_materialArray && human_injury_materialArray.size() > 0) {
                            for(Object str : human_injury_materialArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.HUMAN_INJURY_DATA.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }





                    Object traffic_fee_billsObject = settleCasualtiesImgJSON.get("traffic_fee_bills");
                    if(null != traffic_fee_billsObject) {
                        JSONArray traffic_fee_billsArray = JSONObject.parseArray(JSON.toJSONString(traffic_fee_billsObject));
                        if(null != traffic_fee_billsArray && traffic_fee_billsArray.size() > 0) {
                            for(Object str : traffic_fee_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.TRAFFIC_COST.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }



                    Object disability_indemnity_billsObject = settleCasualtiesImgJSON.get("disability_indemnity_bills");
                    if(null != disability_indemnity_billsObject) {
                        JSONArray disability_indemnity_billsArray = JSONObject.parseArray(JSON.toJSONString(disability_indemnity_billsObject));
                        if(null != disability_indemnity_billsArray && disability_indemnity_billsArray.size() > 0) {
                            for(Object str : disability_indemnity_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.CERTIFICATION_COMPENSATION.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }





                    Object loss_work_fee_billsObject = settleCasualtiesImgJSON.get("loss_work_fee_bills");
                    if(null != loss_work_fee_billsObject) {
                        JSONArray loss_work_fee_billsArray = JSONObject.parseArray(JSON.toJSONString(loss_work_fee_billsObject));
                        if(null != loss_work_fee_billsArray && loss_work_fee_billsArray.size() > 0) {
                            for(Object str : loss_work_fee_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.CHARGE_LOSS_TIME.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }



                    Object follow_up_fee_billsObject = settleCasualtiesImgJSON.get("follow_up_fee_bills");
                    if(null != follow_up_fee_billsObject) {
                        JSONArray follow_up_fee_billsArray = JSONObject.parseArray(JSON.toJSONString(follow_up_fee_billsObject));
                        if(null != follow_up_fee_billsArray && follow_up_fee_billsArray.size() > 0) {
                            for(Object str : follow_up_fee_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.TREATMENT_AFTER_DISCHARGE.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }


                    Object death_certificateObject = settleCasualtiesImgJSON.get("death_certificate");
                    if(null != death_certificateObject) {
                        JSONArray death_certificateArray = JSONObject.parseArray(JSON.toJSONString(death_certificateObject));
                        if(null != death_certificateArray && death_certificateArray.size() > 0) {
                            for(Object str : death_certificateArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.DEATH_CERTIFICATE.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }


                    Object death_fee_billsObject = settleCasualtiesImgJSON.get("death_fee_bills");
                    if(null != death_fee_billsObject) {
                        JSONArray death_fee_billsArray = JSONObject.parseArray(JSON.toJSONString(death_fee_billsObject));
                        if(null != death_fee_billsArray && death_fee_billsArray.size() > 0) {
                            for(Object str : death_fee_billsArray) {
                                String sql = "insert into cars_accident_image(accident_id,type,path) value('"+accidentImgBean.getAccidentId()+"','"+CarAccidentImageEnum.DEATH_CERTIFICATE_COST.getValue()+"','"+str+"')";
                                append(sql);
                            }
                        }
                    }

                }


            }
        }
        System.out.println(imgSqlList);
        log.info(oldDataList);
    }

    public static void append(String content) {
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter("C:\\Users\\xuelin yi\\Desktop\\sql.txt", true);
            writer.write(content+"\r\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
